<?php include("common/conn.php");
if($_SESSION['userid']==""){echo "<script>window.location.href='login.php';</script>";} 

  if($_GET['del']=="ok")
  {
    $area = array(); 
    $area = $_POST['checkbox'];
    
    foreach ($area as $k=>$v)
    {
      $srat1 = mysql_query("DELETE FROM shop where shopid='".$v."'");

      if($srat1 == true) {
        $update_sj = mysql_query("UPDATE goods SET shangjia = 1 where shopid='" . $v . "'");
      }
    }

  }
  //对指定商家的所有商品，销售价、原价 和 规格价，进行批量修改
  if($_GET['modifyPrice']=="ok")
  {
      try {
        $shopid = $_REQUEST['shopid'];
        $rate = $_REQUEST['rate'];
        $query = mysql_query("select * from goods where shopid=".$shopid);

        while ($row = mysql_fetch_assoc($query)) {
          $price = round((100+$rate)/100*$row['price'],1);
          // $yuanjia = round((100+$rate)/100*$row['yuanjia'],1);
          mysql_query("UPDATE goods set price=".$price." where goodsid=".$row['goodsid']);
          //修改该商品所有规格的价格
          $query1 = mysql_query("select * from goodsselect where goodsid=".$row['goodsid']);
          while ($row1 = mysql_fetch_assoc($query1)) {
            $price = (100+$rate)/100*$row1['price'];
            mysql_query("UPDATE goodsselect set price=".$price." where selectid=".$row1['selectid']);
          }
        }
        echo 1;die;
      } catch (Exception $e) {
        
        echo -1;die;
      }   
  
  }

//对指定商家的所有原价进行批量修改
  if($_GET['original_price']=="ok")
  {
      try {   
        $shopid = $_REQUEST['shopid'];
        $rate = $_REQUEST['rate'];
        $query = mysql_query("select * from goods where shopid=".$shopid);
        while ($row = mysql_fetch_assoc($query)) {
          $yuanjia = round((100+$rate)/100*$row['yuanjia'],1);
          mysql_query("UPDATE goods set yuanjia=".$yuanjia." where goodsid=".$row['goodsid']);

        }
        echo 1;die;
      } catch (Exception $e) {
        
        echo -1;die;
      }   
  
  }

  if ($_GET['cancel']='ok') {
    $srat1=mysql_query("DELETE FROM ad where id='".$_GET['id']."'");
  }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>e网批发管理后台</title>
<?php 
// 营业与否
if ($_REQUEST[open]!='') {
  mysql_query("UPDATE shop set open=".$_REQUEST[open]." where shopid=".$_REQUEST[shopid]);
  echo "<script>window.location.href='shequ.php?cityid=0&categroyid=0'</script>";
}
// 营业与否 end

  // 一键审核通过
  if ($_REQUEST['act'] == 'passall') {
      $shopids = $_REQUEST['shopids'];
      $query = mysql_query("UPDATE shop set `check` = 1 where shopid in (".$shopids.")");
      echo "<script>alert('修改成功');window.location.href='shequ.php?cityid=0&categroyid=0'</script>";
  }
  // 一键审核通过 end

// 是否通过审核
if ($_REQUEST['check']!='') {
  mysql_query("UPDATE shop set `check`=".$_REQUEST[check]." where shopid=".$_REQUEST[shopid]);
  echo "<script>window.location.href='shequ.php?cityid=0&categroyid=0'</script>";
}
// 是否通过审核 end
// 选为特价商家
  // if ($_REQUEST[recommend]=='ok') {
  //   mysql_query("UPDATE shop set recommend=1 where shopid=".$_REQUEST[shopid]."");
  //   echo "<script>window.location.href='shequ.php'</script>";
  // }elseif ($_REQUEST[recommend]=='no') {
  //   mysql_query("UPDATE shop set recommend=0 where shopid=".$_REQUEST[shopid]."");
  //   echo "<script>window.location.href='shequ.php'</script>";
  // }
// 选为特价商家 end


// 查询全部社区
$query = mysql_query("select * from city");
while ($row = mysql_fetch_assoc($query)) {
  $citys[] = $row;
}
// 查询全部社区 end

// 查询全部分类
$query = mysql_query("select * from categroy");
while ($row = mysql_fetch_assoc($query)) {
  $categroys[] = $row;
}
// 查询全部分类 end
  ?>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <script type="text/JavaScript">
    <!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<!-- 13924075438 -->
  <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->


  <!-- staqrt subnav -->
  <div id="sub_nav">

    <div id="subnav_wrap" class="container_12">
      <div id="subnav" class=" grid_12">

        <ul>
          <li><a href="shequ.php?cityid=0&categroyid=0" class="sub_nav_active">厂商管理</a></li>
          <li><a href="brokerage.php">佣金管理</a></li>
        </ul>
      </div>

    </div>


  </div>
  <div class="container_12" style="background: #fff; border:0px;">

    <form action="?" method="post">
      <label>市场</label>
      <select class="select citys"  name="cityid">
        <option value="0">全部市场</option>
        <?php foreach ($citys as $v) { ?>
        <option <?php if ($_REQUEST[cityid]==$v[id]) { ?> selected <?php } ?> value="<?php echo $v[id]; ?>"><?php echo $v[name]; ?></option>
        <?php } ?>
      </select>

      <label>分类</label>
      <select class="select categroys" name="categroyid">
        <option value="0">全部分类</option>
        <?php foreach ($categroys as $v) { ?>
        <option <?php if ($_REQUEST[categroyid]==$v[cateid]) { ?> selected <?php } ?> value="<?php echo $v[cateid]; ?>"><?php echo $v[name]; ?></option>
        <?php } ?>
      </select>

      <input  type="text" placeholder="厂商关键字/厂商编码" class="text" value="<?php echo $_REQUEST[keywords]; ?>"name="keywords"/>

      <input name="" type="submit" class="submit" value="搜索厂商" />
      <input type="button" class="submit" value="新增厂商" onclick="add();">
    </form>


  </div>
    <form id="form" action="?cityid=0&categroyid=0&del=ok&shopid=<?php  echo $_REQUEST[shopid]; ?>" method="post">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <th width="5%" scope="col">
            <input type="checkbox" name="checkbox" id="checkbox" class="checkall" /><label for="checkbox"></label></th>
            <th width="13%" scope="col">是否通过审核</th>
            <th width="8%" scope="col">审核状态</th>
            <th width="8%" scope="col">注册手机号码/登录账号</th>
            <th width="8%" scope="col">名称</th>
            <th width="8%" scope="col">厂商代码/登录账号</th>
            <th width="8%" scope="col">地址</th>
            <th width="16%" scope="col">市场</th>
            <th width="8%" scope="col">分类</th>
            <th width="8%" scope="col">商品</th>
            <th width="8%" scope="col">推荐商家</th>
            <th width="15%" scope="col">操作</th>
          </tr>

          <?php 
          mysql_query('SET NAMES UTF8');
          //选为 推荐商家
        if($_GET['recommend']=='no'){
            $sql="update shop set recommend=0 where shopid=".$_GET['shopid'];
            mysql_query($sql);
        }else if($_GET['recommend']=='ok'){
            $sql="update shop set recommend=1 where shopid=".$_GET['shopid'];
            mysql_query($sql);
        }
           //选择精选商家
        if($_GET['jingxuan']=='no'){
            if($_SESSION['name'] == '') {
                echo "<script>alert('未知错误，请联系开发者')</script>";
            } else {
              $fp = fopen("logs/a.txt","a+");
              $msg = "账号id：" . $_SESSION['name'] . "  操作时间：" . date("Y-m-d H:i:s") . "  操作状态：" . "关闭" . "  ip地址：" . $_SERVER["REMOTE_ADDR"] . " 店铺id：" . $_GET['shopid'] . "\r\n";
              fputs($fp,$msg);
              fclose($fp);
              $sql="update shop set jingxuan=0 where shopid=".$_GET['shopid'];
              mysql_query($sql);
            }
        }else if($_GET['jingxuan']=='ok'){
            if($_SESSION['name'] == '') {
                echo "<script>alert('未知错误，请联系开发者')</script>";
            } else {
              $fp = fopen("logs/a.txt","a+");
              $msg = "账号id：" . $_SESSION['name'] . "  操作时间：" . date("Y-m-d H:i:s") . "  操作状态：" . "开启" . "  ip地址：" . $_SERVER["REMOTE_ADDR"] . " 店铺id：" . $_GET['shopid'] . "\r\n";
              fputs($fp,$msg);
              fclose($fp);
              $sql="update shop set jingxuan=1 where shopid=".$_GET['shopid'];
              mysql_query($sql);
            }

        }

$ren="1";
$sex="0";
$perNumber=20; //每页显示的记录数
$page=$_GET['page']; //获得当前的页面值

if (!isset($page)) {
  $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始

$categroyid = '';
$cityidID = '';

if(isset($_GET['categroyid']) && $_GET['categroyid'] != '0' ){
  $categroyid = ' and shop.leimu = "'.$_GET['categroyid'].'"';
}
if(isset($_GET['cityid']) && $_GET['cityid'] != '0' ){
  $cityidID = ' and shop.city = "'.$_GET['cityid'].'"';
}

if($_REQUEST[keywords]!=""){
    /*if($categroyid != ''){
      $categroyid = ' shop.leimu = "'.$_GET['categroyid'].'"';
    }else{
      $categroyid='';
    }
    if($cityidID != ''){
      $cityidID = ' and shop.city = "'.$_GET['cityid'].'" and ';
    }else{
      $cityidID= '';
    }*/

    $count = mysql_query("select count(*) from shop where  1=1  ".$categroyid.$cityidID." and (name like '%".$_REQUEST[keywords]."%'  OR  shop.shop_daima LIKE '%".$_REQUEST[keywords]."%' OR shop.phone LIKE '%".$_REQUEST[keywords]."%') ");

    //获得记录总数
    $rs=mysql_fetch_array($count); 
    $totalNumber=$rs[0];
    $totalPage=ceil($totalNumber/$perNumber); //计算出总页数

    $rert30=mysql_query("select * from shop where  1=1 ".$categroyid.$cityidID." and (name like '%".$_REQUEST[keywords]."%'   OR  shop.shop_daima LIKE '%".$_REQUEST[keywords]."%' OR shop.phone LIKE '%".$_REQUEST[keywords]."%') order by shopid desc limit $startCount,$perNumber");
}else{
  /*if($categroyid != ''){
     $categroyid = 'where shop.leimu = "'.$_GET['categroyid'].'"';
  }else{
    $categroyid='';
  }
  if($cityidID != ''){
    $cityidID = ' and shop.city = "'.$_GET['cityid'].'" ';   
    }else{
      $cityidID= '';
    }*/
  //echo "select count(*) from shop ".$categroyid.$cityidID;die;
  $count =  mysql_query("select count(*) from shop  where 1=1 ".$categroyid.$cityidID);

  //获得记录总数
  $rs=mysql_fetch_array($count); 
  $totalNumber=$rs[0];
  $totalPage=ceil($totalNumber/$perNumber); //计算出总页数

  $rert30=mysql_query("select * from shop  where 1=1 ".$categroyid.$cityidID." order by shopid desc limit $startCount,$perNumber");

}


while($sdg30=mysql_fetch_array($rert30)){
  $ads=mysql_query("select * from ad where shopid='".$sdg30[shopid]."'");
  $wad=mysql_fetch_array($ads);

  $count2=mysql_query("select count(*) from goods where  shopid='".$sdg30[shopid]."' ");
  
// 分类$sdg30[leimu]
  $query=mysql_query("select * from categroy where cateid='".$sdg30[leimu]."'");
  $categroy=mysql_fetch_assoc($query);
// 分类 end

//获得商品记录
  $rs3=mysql_fetch_array($count2); 
  $totalNumber=$rs3[0];
?>

  <tr>

    <td width="5%" scope="col">
      <input type="checkbox" name="checkbox[]" id="checkbox" class="checkbox" value="<?php echo $sdg30[shopid];?>"/></td>
       <td width="13%" scope="col">
        <div class="youse">
        <a href="?cityid=0&categroyid=0&check=1&shopid=<?php echo $sdg30[shopid];?>">是</a>
        </div>
        <div class="youse2">
          <a href="?cityid=0&categroyid=0&check=2&shopid=<?php echo $sdg30[shopid];?>">否</a>
        </div>

       </td>
       <td width="8%" scope="col">
      <?php if ($sdg30[check]==0) { ?>
          待审核
      <?php } ?>
      <?php if($sdg30[check]==1){ ?>
          已通过
      <?php }elseif ($sdg30[check]==2) { ?>
          未通过
      <?php } ?>
       </td>
      <td width="8%" scope="col"><?php echo $sdg30[phone];?></td>
      <td width="8%" scope="col"><?php echo $sdg30[name];?></td>
      <td width="8%" scope="col"><?php echo $sdg30[shop_daima];?></td>
      <td width="8%" scope="col"><?php echo $sdg30[address];?></td>
      <?php
      $count2=mysql_query("select * from city where  id='".$sdg30[city]."' ");
      $city=mysql_fetch_array($count2); 
      ?>
      <td width="16%" scope="col"><?php echo $city[city];?><?php echo $sdg30[quyu];?></td>
      <td width="8%" scope="col"><?php echo $categroy[name];?></td>
      <td width="8%" scope="col"><?php echo $totalNumber;?></td>
       <td width="8%" scope="col">
        <?php if ($sdg30[recommend]==1) {
          echo "是";
        }else{
          echo "否";
        } ?>
      </td>
      <td width="15%" scope="col">
      <div class="youse2"><a href="addshop.php?id=<?php echo $sdg30[shopid];?>">编辑商家</a></div>
      <div class="youse2"><a href="javascript:modifyPrice(<?php echo $sdg30[shopid];?>);">批量修改价格</a></div>
      <div class="youse2"><a href="javascript:original_price(<?php echo $sdg30[shopid];?>);">批量修改原价</a></div>
      <div class="youse"><a href="goods2.php?shopid=<?php echo $sdg30[shopid];?>">管理商品</a></div>
      <div class="youse2"><a href="shopyouhui.php?shopid=<?php echo $sdg30[shopid];?>">商家优惠券</a></div> 
<!--         <?php  
        if($wad!=false){ ?>
        <div class="youse"><a href="?cancel=ok&id=<?php echo $wad[id];?>">取消广告</a></div><?php 
      }else{?>
      <div class="youse2"><a href="addad.php?shopid=<?php echo $sdg30[shopid];?>">选为广告</a></div>
      <?php } ?> -->

       <?php
      if($sdg30[recommend]==1){ ?>
      <div class="youse"><a href="?cityid=0&categroyid=0&recommend=no&shopid=<?php echo $sdg30[shopid];?>&id=<?php echo $wad[id];?>">取消推荐</a></div><?php 
    }else if($sdg30[recommend]==0){?>
    <div class="youse2"><a href="?cityid=0&categroyid=0&recommend=ok&shopid=<?php echo $sdg30[shopid];?>">选为推荐</a></div>
    <?php } ?>

     <?php
      if($sdg30[jingxuan]==1){ ?>
      <div class="youse"><a href="?cityid=0&categroyid=0&jingxuan=no&shopid=<?php echo $sdg30[shopid];?>&id=<?php echo $wad[id];?>">取消精选</a></div><?php 
    }else if($sdg30[jingxuan]==0){?>
    <div class="youse2"><a href="?cityid=0&categroyid=0&jingxuan=ok&shopid=<?php echo $sdg30[shopid];?>">选为精选</a></div>
    <?php } ?>

      <?php
        if($sdg30[open]==1){ ?>
        <div class="youse">
        <a href="?cityid=0&categroyid=0&open=0&shopid=<?php echo $sdg30[shopid];?>">停业</a>
        </div>
      <?php }else if($sdg30[open]==0){?>
      <div class="youse2">
      <a href="?cityid=0&categroyid=0&open=1&shopid=<?php echo $sdg30[shopid];?>">营业</a>
      </div>
      <?php } ?>
  </td>
</tr>

<?php }?>
</table>
</form>
<!-- END TABULAR DATA EXAMPLE -->



<div class="grid_12">

  <div class="clearfix">
      <input  type="button" value="删除选中"  class="submit del" />
      <input  type="button" value="一键审核通过"  class="passall submit" />
  </div>

  <ul id="pagination">

    <?php  

    if ($page > 1) { //上一页 ?>
    <li><a     href="?page=1&keywords=<?php echo $_REQUEST[keywords]; ?>&cityid=<?php echo  $_REQUEST[cityid];?>&categroyid=<?php echo $_REQUEST[categroyid]; ?>">首页</a></li>
    <li><a     href="?page=<?php echo ($page-1); ?>&keywords=<?php echo $_REQUEST[keywords]; ?>&cityid=<?php echo  $_REQUEST[cityid];?>&categroyid=<?php echo $_REQUEST[categroyid]; ?>">上一页</a></li>
    <?php }

    ?>

    <li class="active"><?php echo $page;?></li>

    <?php

    if ($_GET[page]!=$totalPage) { //下一页 ?>
    <li><a     href="?page=<?php echo ($page+1); ?>&keywords=<?php echo $_REQUEST[keywords]; ?>&cityid=<?php echo  $_REQUEST[cityid];?>&categroyid=<?php echo $_REQUEST[categroyid]; ?>">下一页</a></li>
    <?php }

    if ($totalPage>1) { //下一页 ?>
    <li><a     href="?page=<?php echo $totalPage; ?>&keywords=<?php echo $_REQUEST[keywords]; ?>&cityid=<?php echo  $_REQUEST[cityid];?>&categroyid=<?php echo $_REQUEST[categroyid]; ?>">尾页</a></li>

    <?php }?>
    <input type="text" class="text skip_val" style="width: 20px;height:12px;margin:0 5px;"><button class="submit skip">跳转</button>

  </ul>

</div>           

</div>
<input type="hidden" id="shopid" value="<?php echo $_REQUEST[shopid]; ?>">
<!-- end content wrap -->

</body>
        <!-- 对话框插件 -->
<script src="js/artDialog/jquery.artDialog.js?skin=twitter"></script>
<script src="js/artDialog/plugins/iframeTools.js"></script>
<!-- 表单库插件 -->
<script type="text/javascript" src="js/jquery.form.min.js"></script>
<script type="text/javascript">

  var shopid=$('#shopid').val();

  function add(){
    window.location.href="addshop.php?shopid="+shopid;
  }

  var cityVal = $('.citys').val();
  
  // 查询社区
  $('.citys').change(function(){
    var categroyid = $('.categroys').val();
    var cityid = $(this).val();
    window.location.href="shequ.php?cityid="+cityid+"&categroyid="+categroyid;
  });
  // 查询社区 end
  
  // 查询分类
  $('.categroys').change(function(){
    var cityid = $('.citys').val();
    var categroyid = $(this).val();
    window.location.href="shequ.php?cityid="+cityid+"&categroyid="+categroyid;
  });
  // 查询分类 end

  // 一键审核通过
  $('.passall').click(function(){
      var shopid = '';
      $(".checkbox").each(function(){
          if ($(this).prop("checked")) {
              shopid += ','+$(this).val();
          };
      });
      shopid = shopid.substring(1);
      window.location.href = 'shequ.php?cityid=0&categroyid=0&act=passall&shopids='+shopid;
  })
  // 一键审核通过 end


  // 删除 
  $('.del').click(function(){
    $('#form').submit();
  })
  // 删除 end

  // 页面跳转 
  $('.skip').click(function(){
    var page = $('.skip_val').val();
    var url = "shequ.php?page="+page+"&keywords=<?php echo $_REQUEST[keywords]; ?>&cityid=<?php echo  $_REQUEST[cityid];?>&categroyid=<?php echo $_REQUEST[categroyid]; ?>"
    // var url = "shequ.php?page="+page;
    window.location.href = url;
  })
  // 页面跳转 end
  
  //批量修改价格
   function  modifyPrice(shopid){
    $.dialog({
          title: '批量修改价格',
          content: "<form  id='purview'><input  id='modifyPrice_id'  type='text' placeholder='百分比（如：-5 或 5）' class='text'/><br/><span id='tips'  style='color:red'></span></form>",
          ok: function () {
              $("#tips").html("请稍后，批量修改中...");
              var rate = $("#modifyPrice_id").val();
              $("#purview").ajaxSubmit({
                  url:'?modifyPrice=ok&rate='+rate+'&shopid='+shopid,
                  type:"get",
                  success:function(data){
                      $("#tips").html("批量修改成功");
                      alert('更新成功');
                  }           
              });
              return false;
          },
          okVal:'提交',
          cancel: true,
          cancelVal:'取消',
          close:function(){
             window.location.href = "?cityid=0&categroyid=0&time="+new Date().getTime();
          }
      });
   }

  //批量修改价格
   function  original_price(shopid){
    $.dialog({
          title: '批量修改原价',
          content: "<form  id='purview'><input  id='original_price'  type='text' placeholder='百分比（如：-5 或 5）' class='text'/><br/><span id='tips'  style='color:red'></span></form>",
          ok: function () {
              $("#tips").html("请稍后，批量修改中...");
              var rate = $("#original_price").val();
              $("#purview").ajaxSubmit({
                  url:'?original_price=ok&rate='+rate+'&shopid='+shopid,
                  type:"get",
                  success:function(data){
                      $("#tips").html("批量修改成功");
                      alert('更新成功');
                  }           
              });
              return false;
          },
          okVal:'提交',
          cancel: true,
          cancelVal:'取消',
          close:function(){
             window.location.href = "?cityid=0&categroyid=0&time="+new Date().getTime();
          }
      });
   }
</script>
</html>
